package com.azure.service.Report;

import com.azure.entity.AProductDailyReport;
import com.azure.entity.ATurnoverDailyReport;
import com.azure.entity.ATurnoverMonthlyReport;
import com.azure.mapper.bigstore.TOrderMapper;
import com.azure.mapper.bigstore.TUserMapper;
import com.azure.mapper.report.ATurnoverDailyReportMapper;
import com.azure.mapper.report.ATurnoverMonthlyReportMapper;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhuaibin on 2018/3/13.
 */
@Service
public class TurnoverReportService {

    @Autowired
    private TOrderMapper tOrderMapper;
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private ATurnoverDailyReportMapper aTurnoverDailyReportMapper;
    @Autowired
    private ATurnoverMonthlyReportMapper aTurnoverMonthlyReportMapper;

    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyyMM");

    public void totalDailyReport(LocalDate date){
        List<JSONObject> jsonObjects = tOrderMapper.totalDailyReport(date);
        if(jsonObjects==null || jsonObjects.size() == 0){
            System.out.println("=== TurnoverReportService totalDailyReport  !!!empty list!!! tOrderMapper.totalDailyReport() ===");
            return;
        }
        Map<Long,Integer> uid_total_map = new HashMap<>();
        List<Long> uids = new ArrayList<>();
        for(JSONObject object : jsonObjects){
            uid_total_map.put(Long.valueOf(object.getAsString("uid")), Integer.valueOf(object.getAsString("total")));
            uids.add(Long.valueOf(object.getAsString("uid")));
        }
        //uid--province
        List<JSONObject> uidProvinceList = tUserMapper.selectUidProvinceByIds(uids);
        Map<Long, String> uid_province_map = new HashMap<>();
        for(JSONObject object : uidProvinceList){
            uid_province_map.put(Long.valueOf(object.getAsString("id")),object.getAsString("province").replaceAll("省",""));
        }
        //province--total
        Map<String,Integer> province_total_map = new HashMap<>();
        for(Long uid : uid_province_map.keySet()){
            int num = province_total_map.get(uid_province_map.get(uid))==null ? 0 : province_total_map.get(uid_province_map.get(uid));
            province_total_map.put( uid_province_map.get(uid) , num+uid_total_map.get(uid));
        }
        System.out.println("===== TurnoverReportService totalDailyReport province_total_map:"+province_total_map);
        for(String province : province_total_map.keySet()){
            aTurnoverDailyReportMapper.insert(new ATurnoverDailyReport(dateTimeFormatter.format(date),province, province_total_map.get(province)));
        }
    }

    public void totalMonthlyReport(LocalDate startDate, LocalDate stopDate){
        List<JSONObject> jsonObjects = tOrderMapper.totalMonthlyReport(startDate,stopDate);
        if(jsonObjects==null || jsonObjects.size() == 0){
            System.out.println("=== TurnoverReportService totalMonthlyReport  !!!empty list!!! tOrderMapper.totalMonthlyReport() ===");
            return;
        }
        Map<Long,Integer> uid_total_map = new HashMap<>();
        List<Long> uids = new ArrayList<>();
        for(JSONObject object : jsonObjects){
            uid_total_map.put(Long.valueOf(object.getAsString("uid")), Integer.valueOf(object.getAsString("total")));
            uids.add(Long.valueOf(object.getAsString("uid")));
        }
        //uid--province
        List<JSONObject> uidProvinceList = tUserMapper.selectUidProvinceByIds(uids);
        Map<Long, String> uid_province_map = new HashMap<>();
        for(JSONObject object : uidProvinceList){
            uid_province_map.put(Long.valueOf(object.getAsString("id")),object.getAsString("province").replaceAll("省",""));
        }
        //province--total
        Map<String,Integer> province_total_map = new HashMap<>();
        for(Long uid : uid_province_map.keySet()){
            int num = province_total_map.get(uid_province_map.get(uid))==null ? 0 : province_total_map.get(uid_province_map.get(uid));
            province_total_map.put( uid_province_map.get(uid) , num+uid_total_map.get(uid));
        }
        System.out.println("===== TurnoverReportService totalMonthlyReport province_total_map:"+province_total_map);
        for(String province : province_total_map.keySet()){
            aTurnoverMonthlyReportMapper.insert(new ATurnoverMonthlyReport(dateTimeFormatter2.format(startDate),province, province_total_map.get(province)));
        }
    }

    public Map<String,Object> getDailyTurnoverData(String date){
        Map<String,Object> map = new HashMap<>();
        List<String> provinceNames = new ArrayList<>();
        List<Integer> turnovers = new ArrayList<>();

        List<ATurnoverDailyReport> dailyTurnoverData = aTurnoverDailyReportMapper.getDailyTurnoverData(date);
        List<JSONObject> turnoverLineData = aTurnoverDailyReportMapper.getTurnoverLine(dateTimeFormatter.format(LocalDate.now().minusMonths(1)));
        if(dailyTurnoverData==null || dailyTurnoverData.size()==0){
            return map;
        }
        for(ATurnoverDailyReport aTurnoverDailyReport : dailyTurnoverData){
            provinceNames.add("\""+aTurnoverDailyReport.getProvince()+"\"");
            turnovers.add(aTurnoverDailyReport.getTurnover());
        }
        map.put("dailyProvinceNames",provinceNames);
        map.put("dailyTurnovers",turnovers);
        map.put("dailyDate","\""+date+"日数据\"");
        if(turnoverLineData==null || turnoverLineData.size()==0){
            System.out.println("=== TurnoverReportService dailyTurnoverData  !!!empty list!!! tOrderMapper.getTurnoverLine() ===");
            return map;
        }
        List<String> dateList = new ArrayList<>();
        List<Integer> dataList = new ArrayList<>();
        for(JSONObject object : turnoverLineData){
            dateList.add("\""+object.getAsString("date")+"\"");
            dataList.add(Integer.valueOf(object.getAsString("turnover")));
        }
        map.put("turnoverLineDate",dateList);
        map.put("turnoverLineData",dataList);
        return map;
    }
    public Map<String,Object> getMonthlyTurnoverData(String date){
        Map<String,Object> map = new HashMap<>();
        List<String> provinceNames = new ArrayList<>();
        List<Integer> turnovers = new ArrayList<>();

        List<ATurnoverMonthlyReport> monthlyTurnoverData = aTurnoverMonthlyReportMapper.getMonthlyTurnoverData(date);
        if(monthlyTurnoverData==null || monthlyTurnoverData.size()==0){
            return map;
        }
        for(ATurnoverMonthlyReport aTurnoverMonthlyReport : monthlyTurnoverData){
            provinceNames.add("\""+aTurnoverMonthlyReport.getProvince()+"\"");
            turnovers.add(aTurnoverMonthlyReport.getTurnover());
        }
        map.put("monthlyProvinceNames",provinceNames);
        map.put("monthlyTurnovers",turnovers);
        map.put("monthlyDate","\""+date+"月数据\"");
        return map;
    }
}
